package com.zsw.procure_system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.zsw.procure_system.mapper.CollectMapper;
import com.zsw.procure_system.mapper.OrderinfoMapper;
import com.zsw.procure_system.pojo.Collect;
import com.zsw.procure_system.pojo.Orderinfo;
import com.zsw.procure_system.pojo.result.ApiResult;
import com.zsw.procure_system.service.OrderinfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.management.Query;
import java.sql.Time;
import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderinfoMapper orderinfoMapper;
    @Autowired
    private CollectMapper collectMapper;
    @Autowired
    private OrderinfoService orderinfoService;


    @GetMapping("/list")
    public ApiResult<List<Orderinfo>> list(@RequestParam int page) {
        PageHelper.startPage(page, 10);
        List<Orderinfo> orderinfosList = orderinfoMapper.selectList(null);
        Integer total = orderinfoMapper.selectCount(null);
        ApiResult<List<Orderinfo>> list = new ApiResult<>();
        list.setResultValue(orderinfosList);
        list.setTotal(total);
        list.setCode(200);
        list.setSuccess(true);
        return list;
    }

    @GetMapping("/collect")
    public ApiResult<List<Collect>> collect(@RequestParam int page) {
        PageHelper.startPage(page, 10);
        List<Collect> collectList = collectMapper.selectList(null);
        Integer total = collectMapper.selectCount(null);
        ApiResult<List<Collect>> list = new ApiResult<>();
        list.setResultValue(collectList);
        list.setTotal(total);
        list.setCode(200);
        list.setSuccess(true);
        return list;
    }

    @GetMapping("/changeStatus")
    public ApiResult<Boolean> changeStatus(@RequestParam String ids) {
        String[] split = ids.split(",");

        UpdateWrapper<Orderinfo> orderinfoUpdateWrapper = new UpdateWrapper<>();
        orderinfoUpdateWrapper.set("huizongStatus", "2");
        orderinfoUpdateWrapper.in("id", split);
        orderinfoService.update(orderinfoUpdateWrapper);

        //同时插入collect表中
        Collect collect = new Collect();

        Random random = new Random();
        // 生成一个9位的随机数
        int randomId = 100000000 + random.nextInt(900000000); // 生成100000000到999999999之间的随机整数
        collect.setId(randomId);
        collect.setIds(ids);
        int randomOrderNum = 100000000 + random.nextInt(900000000); // 生成100000000到999999999之间的随机整数
        collect.setOrdernum(randomOrderNum);
        collect.setOperator("zhagnsan");
        collect.setPhone("18226058462");
        collect.setTime(new Date());
        collect.setTotalprice(1688);
        collectMapper.insert(collect);
        return ApiResult.ok();
    }

    @GetMapping("/cancel")
    public ApiResult<Boolean> cancel(@RequestParam int id) {
        Collect collect = collectMapper.selectById(id);
        String ids = collect.getIds();
        String[] split = ids.split(",");

        UpdateWrapper<Orderinfo> orderinfoUpdateWrapper = new UpdateWrapper<>();
        orderinfoUpdateWrapper.set("huizongStatus", "1");
        orderinfoUpdateWrapper.in("id", split);
        boolean update = orderinfoService.update(orderinfoUpdateWrapper);

        collectMapper.deleteById(id);
        return ApiResult.ok();
    }

//    @GetMapping("/detail")
//    public ApiResult<List<Collect>> detail(@RequestParam int page) {
//
//        return list;
//    }
}
